//index.js
var app = getApp();
var config = require('../../config')
var util = require('../../utils/util.js')

Page({
  data: {
    // 
    today: "",
    // 城市名
    location: "",
    // 地区名
    county: "",
    // 今天天气情况数组
    weatherData: '',
    // 生活指数
    dress: '',
    // 图片数组
    sliderList: [{
        selected: true,
        imageSource: '../../source/images/lb0.jpg'
      },
      {
        selected: false,
        imageSource: '../../source/images/lb1.jpg'
      },
      {
        selected: false,
        imageSource: '../../source/images/lb2.jpg'
      },
      {
        selected: false,
        imageSource: '../../source/images/lb3.jpg'
      },
      {
        selected: false,
        imageSource: '../../source/images/lb4.jpg'
      },
      {
        selected: false,
        imageSource: '../../source/images/lb5.jpg'
      },
      {
        selected: false,
        imageSource: '../../source/images/lb6.jpg'
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('生命周期函数--监听页面加载')
    this.setData({
      today: app.globalData.day //更新当前日期
    });
    this.getLocation();
  },

  // 定位当前城市的函数
  getLocation: function () {
    console.log("正在定位城市");
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //当前的经度和纬度
        let latitude = res.latitude
        let longitude = res.longitude
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}',
          success: res => {
            console.log('11111',res);
            if (res.data.status != 'ok'){return;}
            app.globalData.defaultCity = res.data.result.ad_info.city;
            app.globalData.defaultCounty = res.data.result.ad_info.district;
            that.setData({
              location: app.globalData.defaultCity,
              county: app.globalData.defaultCounty
            });
            console.log('1111__111',app.globalData.defaultCity);
            console.log('1111_2222',app.globalData.defaultCounty);
            //that.getWeather();
            //that.getAir();
          }
        })
      }
    })
  },
  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function () {
    console.log('生命周期函数--监听页面显示')
    this.setData({
      location: app.globalData.defaultCity,
      county: app.globalData.defaultCounty
    });
    // this.getWeather();
    this.getAir();
  },
  //获取天气
  getWeather: function (e) {
    var city = this.data.location.slice(0, 2); //分割字符串
    var that = this;
    var url = "https://free-api.heweather.com/s6/weather";
    var param = {
      key: "c5bfaf6bf312400891a8bd572b751dec",
      location: city
    };
    //发出请求
    wx.request({
      url: url,
      data: param,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('2222',res);
        app.globalData.weatherData = res.data.HeWeather6[0];
        that.setData({
          weatherData: app.globalData.weatherData.now, //今天天气情况数组 
          dress: res.data.HeWeather6[0].lifestyle[1] //生活指数
        });
      }
    })
  },

  getAir: function (e) {
    var city = this.data.location.slice(0, 2); //分割字符串
    var that = this;
    var url = "https://free-api.heweather.com/s6/air/now";
    var param = {
      key: "c5bfaf6bf312400891a8bd572b751dec",
      location: city
    };
    //发出请求
    wx.request({
      url: url,
      data: param,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('3333',res);
        app.globalData.air = res.data.HeWeather6[0].status;
        that.setData({
          air: app.globalData.air
        });
      }
    })
  },

  // 轮播图 滚动事件 下方按钮跟随变化
  switchTab: function(e) {
    var sliederList = this.data.sliderList;
    var i, item;
    for (i = 0; item = sliederList[i]; ++i) {
      item.selected = e.detail.current == i;
    }
    this.setData({
      sliderList: sliederList
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('生命周期函数--监听页面隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('生命周期函数--监听页面卸载')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(' 页面相关事件处理函数--监听用户下拉动作')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('页面上拉触底事件的处理函数')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(' 用户点击右上角分享')
  }
})